﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Titul.Help.WebUI.Controllers
{
    public partial class ProductsController : Controller
    {
        private Storages.ProductStorage _storage;
        public ProductsController()
        {
            _storage = new Storages.ProductStorage();
        }

        [HttpGet]
        [AllowAnonymous]
        //[OutputCache(Duration = 60)]
        public virtual ActionResult Index()
        {
            return View(_storage.All.OrderBy(x => x.Name).Take(3));
        }

        [HttpGet]
        [AllowAnonymous]
        public virtual ViewResult Details(string seoUrl)
        {
            return View(_storage.Single(seoUrl));
        }

        [HttpGet]
        public virtual ViewResult Edit(int? id = null)
        {
            if (id == null)
                return View(new Models.Product());
            else
            {
                var model = _storage.Single((int)id);
                return View(model);
            }
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public virtual ActionResult Edit(Models.Product product)
        {
            if (!ModelState.IsValid)
                return View(product);
            else
            {
                _storage.Edit(product);
                return RedirectToAction(MVC.Products.Details(product.SeoUrl));
            }
        }
    }
}